/**
 * hplusUtil.js  工具js
 * author king
     功能说明：
     创建日期：2015-09-24
     修改记录：
     备注说明：
 */

var hui ;

var HUI = function(){};

HUI.prototype = {
		/**
		 *  分页处理
		 * @param url      请求地址
		 * @param datas  请求参数, {参数1:value1,....}
		 * @param divId   显示数据的表格ID
		 * @param columnDefine   在显示详情时，最原始数据名称做替换{字段名:中文对照名,...}
		 * @param buttons 自定义按钮事件 例如
		 * 			text+="&lt;p&gt;";
			    	text+="&lt;button type="button" class="btn btn-w-m btn-primary" onclick="common.showMsg(\"info\", null , \"操作1成功\");$(\"#description .modal-footer button\").click();"&gt;操作1&lt;/button&gt;&nbsp;";
			    	text+="&lt;/p&gt;";
		 */
		 pager:function( url , datas , divId , columnDefine , formatters , buttons ){
			 
			 $('#'+divId ).bootgrid("destroy");
				var grid = $('#'+divId ).bootgrid({
					ajax:true,
				    selection: true,
				    multiSelect: true,
				    rowSelect: true,
				    sorting:false,
				    rowCount:[10,15,20],
				    formatters:formatters,
					post:function(){
						return datas;
					},
				    url:url,	
				}).on("click.rs.jquery.bootgrid", function (e,selectedRows,row){
					
					var text='';
			    	for (key in row){
			    		text+='<p>';
			    		var val = row[key];
			    		if( val == null )
			    		{
			    			val = '';
			    		}
			    		
			    		val = ''+val;
			    		
			    		if( val.indexOf('|') >=0 )
		    			{
		    				val = '<br/>'+val.split('|').join('<br/>');
		    			}	
			    		
			    		if(columnDefine&&columnDefine[key])
			    		{
			    			text+= columnDefine[key] + '  :   ' +val;	
			    		}
			    		else
			    		{
			    			text+= key + '  :   ' + val ;	
			    		}	
			    		text+='</p>';
			    	}
			    	
			    	text+='<span class="data" style="display:none">'+JSON.stringify(row)+'</span>';
			    	
			    	if( buttons )
			    	{
			    		text+=buttons;
			    	}	
					
					$('#pop_win .modal-body').empty();
					$('#pop_win .modal-body').append(text);
					$('#pop_win').modal('show');
					
			    }).on("selected.rs.jquery.bootgrid", function (e, selectedRows)
			    {
			        /*var row, quantity;
			        for (var i = 0; i < selectedRows.length; i++)
			        {
			            row = selectedRows[i];
			            if (!data.contains(function (item) { return item.id === row.id; }))
			            {
			                quantity = grid.find("#" + row.id + "-quantity").val();
			                data.push({ id: row.id, quantity: quantity || 0 });
			            }
			        }*/
			    }).on("deselected.rs.jquery.bootgrid", function (e, deselectedRows)
			    {
			        /*var row;
			        for (var i = 0; i < deselectedRows.length; i++)
			        {
			            row = deselectedRows[i];
			            for (var j = 0; j < data.length; j++)
			            {
			                if (data[j].id === row.id)
			                {
			                    data.splice(j, 1);
			                    return;
			                }
			            }
			        }*/
			    });
				
		 }, 
		 /**
		  *  显示toastr消息提示框
		  * @param type    info , warning , error
		  * @param title     标题
		  * @param content  消息内容
		  */
		 showMsg:function( type , title , content ){
			 toastr.options = {
					  "closeButton": true,
					  "debug": false,
					  "progressBar": false,
					  "positionClass": "toast-top-full-width",
					  "onclick": null,
					  "showDuration": "400",
					  "hideDuration": "500",
					  "timeOut": "1000",
					  "extendedTimeOut": "500",
					  "showEasing": "swing",
					  "hideEasing": "linear",
					  "showMethod": "fadeIn",
					  "hideMethod": "fadeOut"
			  };
			 if( type == 'info' )
			 {
				 toastr.info( title , content );
			 }
			 else if( type == 'warning' )
			 {
				 toastr.warning( title , content );
			 }	 
			 else if( type == 'error' )
			 {
				 toastr.error( title , content );
			 }	
			 else
			 {
				 toastr.info( title , content );
			 }	 
		 },
		 /**
		  * 显示确认窗口
		  * @param title  消息标题
		  * @param text  消息内容
		  * @param functionStr 调用的方法：例如 XXXX();
		  */
		 showConfirm:function(  title , text , functionStr ){
			  swal({
			        title: title,
			        text: text,
			        showCancelButton: true,
			        confirmButtonColor: "#DD6B55",
			        confirmButtonText: '确定' ,
			        closeOnConfirm: false
			    }, 
			    function(a) {
		            if (a) {
		            	try
		            	{
		            		if( functionStr )
		            		{
			            		var flag = eval(functionStr);
			            		if( flag )
			            		{
			            			swal('','操作成功','success');
			            		}
			            		else
			            		{
			            			swal('','操作失败','error');
			            		}	
		            		}	
		            	}
		            	catch(ex)
		            	{
		            		swal('异常',ex,'error');
		            	}
		            }
		            else 
		            {
		            	
		            }
		        });
		 }
};

hui = new HUI();